﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Components;
using BAL.Entities;
using System.Data;
using DigitalAnts.App_Code.BAL.Components;
using DigitalAnts.App_Code.BAL.Utils;

namespace DigitalAnts.Admin
{
    public partial class UpdateProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Response.Write(Request["ProductID"]);
                GetProduct();
            }
        }
        public void GetProduct()
        { 
            int productid = Convert.ToInt32(Request["ProductID"]);
            clsProduct obj = new clsProduct();
            obj.ProductID = productid;
            DataTable dt = clsProductBAL.doProductSelectID(obj);
            txt_product.Text = dt.Rows[0]["colProduct"].ToString();
            txtAntiThiefDevice.Text = dt.Rows[0]["colAntiThiefDevice"].ToString();
            txt_color.Text = dt.Rows[0]["colColor"].ToString();
            txtisnew.Text = dt.Rows[0]["IsNew"].ToString();
            txt_price.Text = dt.Rows[0]["colPrice"].ToString();
            txt_km.Text = dt.Rows[0]["colKilometersTraveled"].ToString();
            ShowUser();
            showDropProducer();
            showDropCity();
            showDropFuel();
        }
            
        public void UpdateProductInfo()
        {
            clsProduct obj = new clsProduct();
            if (fulavatar.FileName != null)
            {
                obj.Avatar = fulavatar.FileName;
                clsUploadImage.uploadProductImage(fulavatar);
               
            }
            obj.ProductID = Convert.ToInt32(Request["ProductID"]);
            obj.ProductName = txt_product.Text.Trim();
            obj.AntiThiefDevice = Convert.ToInt32(txtAntiThiefDevice.Text);
            obj.Userid = Convert.ToInt32(ddluser.SelectedValue);
            obj.Color = txt_color.Text.Trim();
            obj.ProducerID = Convert.ToInt32(ddlProducer.SelectedValue);
            obj.Price = Convert.ToDouble(txt_price.Text.Trim());
            obj.KilometersTraveled = int.Parse(txt_km.Text.Trim());
            obj.FuelID = Convert.ToInt32(ddlfuel.SelectedValue);
            obj.CityID = Convert.ToInt32(ddlcity.SelectedValue);
            if (clsProductBAL.doUpdateProduct(obj) == true)
            {
                lbl_mess.Text = "Cập nhật thông tin thành công.";
            }
            else
            {
                lbl_mess.Text = "Cập nhật thông tin thất bại.";
            }
            
        }

        protected void btt_update_Click(object sender, EventArgs e)
        {
                UpdateProductInfo();
        }

        public void ShowUser()
        {
            int productid = Convert.ToInt32(Request["ProductID"]);
            clsProduct obj = new clsProduct();
            obj.ProductID = productid;
            DataTable dt = clsProductBAL.doProductSelectID(obj);
            clsUser obj1 = new clsUser();
            obj1.UserID = Convert.ToInt32(dt.Rows[0]["colUserID"].ToString());
            ddluser.DataSource = clsUserBAL.doGetAllUser();
            ddluser.DataTextField = "colUserName";
            ddluser.DataValueField = "colUserID";
            ddluser.DataBind();
            ddluser.SelectedValue = clsUserBAL.doGetAllUserList(obj1).Rows[0]["colUserID"].ToString();
        }

        public void showDropProducer()
        {
            int productid = Convert.ToInt32(Request["ProductID"]);
            clsProduct obj = new clsProduct();
            obj.ProductID = productid;
            DataTable dt = clsProductBAL.doProductSelectID(obj);
            clsProducer objp = new clsProducer();
            objp.ProducerID = Convert.ToInt32(dt.Rows[0]["colProducerID"].ToString());

            ddlProducer.DataSource = clsProducerBAL.doProducerSelectList();
            ddlProducer.DataTextField = "colProducer";
            ddlProducer.DataValueField = "colProducerID";
            ddlProducer.DataBind();
            ddlProducer.SelectedValue = clsProducerBAL.doProductSelectID(objp).Rows[0]["colProducerID"].ToString();
        }

        public void showDropCity()
        {
            int productid = Convert.ToInt32(Request["ProductID"]);

            clsProduct obj = new clsProduct();
            obj.ProductID = productid;
            DataTable dt = clsProductBAL.doProductSelectID(obj);
            
            clsCity objc = new clsCity();
            objc.CityID = Convert.ToInt32(dt.Rows[0]["colCityID"].ToString());

            ddlcity.DataSource = clsCityBAL.doCitySelectList();
            ddlcity.DataTextField = "CityName";
            ddlcity.DataValueField = "CityID";
            ddlcity.DataBind();
            ddlcity.SelectedValue = clsCityBAL.doCitySelectListById(objc).Rows[0]["CityID"].ToString();
        }

        public void showDropFuel()
        {
            int productid = Convert.ToInt32(Request["ProductID"]);
            clsProduct obj = new clsProduct();
            obj.ProductID = productid;
            DataTable dt = clsProductBAL.doProductSelectID(obj);
            clsFuel objf = new clsFuel();
            objf.FuelID = Convert.ToInt32(dt.Rows[0]["colFuelID"].ToString());

            ddlfuel.DataSource = clsFuelBAL.doFuelSelectList();
            ddlfuel.DataTextField = "Fuel";
            ddlfuel.DataValueField = "FuelID";
            ddlfuel.DataBind();
            ddlfuel.SelectedValue = clsFuelBAL.doFuelSelectListByID(objf).Rows[0]["FuelID"].ToString();

        }
    }
}